package admin

import (
	"github.com/gin-gonic/gin"
	"mojieshi/define"
	"mojieshi/models"
	"net/http"
	"strconv"
	"time"
)

// 商户分类list
func BusinessCateList(c *gin.Context) {
	var jsonData Newdata
	err := c.ShouldBindJSON(&jsonData)
	if err != nil {
		define.Response.ResultFail(c, 10000, define.JsonData, "")
		return
	}
	var list []models.MBusinessCategory
	var count int64
	if jsonData.Page == 0 {
		jsonData.Page = -1
	}
	if jsonData.Limit == 0 {
		jsonData.Limit = -1
	}
	offsetVal := (jsonData.Page - 1) * jsonData.Limit
	if jsonData.Page == -1 && jsonData.Limit == -1 {
		offsetVal = -1
	}
	err = models.DB.Model(new(models.MBusinessCategory)).Where(models.MBusinessCategory{ID: jsonData.Id, Aid: jsonData.Aid}).Count(&count).Limit(jsonData.Limit).Offset(offsetVal).Order("id desc").Find(&list).Error
	if err != nil {
		define.Response.ResultFail(c, 10001, err.Error(), "")
		return
	}
	c.JSON(http.StatusOK, gin.H{
		"success": true,
		"code":    0,
		"data": map[string]interface{}{
			"list":  list,
			"count": count,
		},
	})
}

// 商户分类新增or修改
func BusinessCateAdd(c *gin.Context) {
	//获取传值
	var jsonData models.MBusinessCategory
	err := c.ShouldBindJSON(&jsonData)
	if err != nil {
		c.JSON(http.StatusOK, gin.H{
			"code": 10000,
			"msg":  err,
		})
		return
	}
	if jsonData.ID > 0 {
		//修改数据
		var editData = map[string]interface{}{
			"name":   jsonData.Name,
			"pic":    jsonData.Pic,
			"status": jsonData.Status,
			"sort":   jsonData.Sort,
		}
		err = models.DB.Model(new(models.MBusinessCategory)).Where("id=?", jsonData.ID).Updates(&editData).Error
		if err != nil {
			c.JSON(http.StatusOK, gin.H{
				"code": 10003,
				"msg":  err.Error(),
			})
			return
		}
		//写入日志
		userId, _ := strconv.Atoi(c.GetHeader("userId")) //将字符串转为int
		userName, bid := models.UserName(userId)
		logData := "管理员：" + userName + "，修改了商户分类:" + strconv.Itoa(jsonData.ID) + "的内容"
		models.LogAdd(3, "修改商户分类", userId, logData, bid, jsonData.Aid)
	} else {
		var data models.MBusinessCategory
		data.Name = jsonData.Name
		data.Pic = jsonData.Pic
		data.Status = jsonData.Status
		data.Aid = jsonData.Aid
		data.Sort = jsonData.Sort
		data.Createtime = int(time.Now().Unix())
		err = models.DB.Model(new(models.MBusinessCategory)).Create(&data).Error
		if err != nil {
			c.JSON(http.StatusOK, gin.H{
				"code": 10001,
				"msg":  err.Error(),
			})
			return
		}
		//写入日志
		userId, _ := strconv.Atoi(c.GetHeader("userId")) //将字符串转为int
		userName, bid := models.UserName(userId)
		logData := "管理员：" + userName + "，新增了一个商户分类:" + jsonData.Name
		models.LogAdd(2, "新增商户分类", userId, logData, bid, jsonData.Aid)
	}
	c.JSON(http.StatusOK, gin.H{
		"success": true,
		"code":    0,
		"msg":     "成功！",
	})
}

//删除商户分类

func BusinessCateDel(c *gin.Context) {
	var jsonData models.MBusinessCategory
	err := c.ShouldBindJSON(&jsonData)
	if err != nil {
		c.JSON(http.StatusOK, gin.H{
			"code": 10000,
			"msg":  err,
		})
		return
	}
	var data models.MBusinessCategory
	models.DB.Model(new(models.MBusinessCategory)).Where("id=?", jsonData.ID).First(&data)
	tx := models.DB.Begin()

	//删除
	err = tx.Delete(&models.MBusinessCategory{}, jsonData.ID).Error
	if err != nil {
		c.JSON(http.StatusOK, gin.H{
			"code": 10004,
			"msg":  err.Error(),
		})
		tx.Rollback()
		return
	}
	tx.Commit()

	//写入日志
	userId, _ := strconv.Atoi(c.GetHeader("userId")) //将字符串转为int
	userName, bid := models.UserName(userId)
	logData := "管理员：" + userName + "，删除了一个商户分类:" + data.Name
	models.LogAdd(4, "删除商户分类", userId, logData, bid, jsonData.Aid)
	c.JSON(http.StatusOK, gin.H{
		"success": true,
		"code":    0,
		"msg":     "成功！",
	})
}

// 绑定商品分类
func BusinessCateShopCate(c *gin.Context) {
	var jsonData models.MBusinessCategory
	err := c.ShouldBindJSON(&jsonData)
	if err != nil {
		define.Response.ResultFail(c, 10000, define.JsonData, "")
		return
	}
	var editData = map[string]interface{}{
		"cate_id": jsonData.CateId,
	}
	err = models.DB.Model(new(models.MBusinessCategory)).Where("id=?", jsonData.ID).Updates(&editData).Error
	if err != nil {
		c.JSON(http.StatusOK, gin.H{
			"code": 10003,
			"msg":  err.Error(),
		})
		return
	}
	//写入日志
	userId, _ := strconv.Atoi(c.GetHeader("userId")) //将字符串转为int
	userName, bid := models.UserName(userId)
	logData := "管理员：" + userName + "，修改了商户分类:" + strconv.Itoa(jsonData.ID) + "的商品绑定"
	models.LogAdd(3, "修改商户分类", userId, logData, bid, jsonData.Aid)
	c.JSON(http.StatusOK, gin.H{
		"success": true,
		"code":    0,
		"msg":     "成功！",
	})
}
